﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace PineLake.Drmwe.Lib.Regular
{
    /// <summary>
    /// 正则数据验证类
    /// </summary>
    public class Validation
    {
        private static Match Mch;
        private static Regex RegNumber = new Regex("^[0-9]+$");                       // 是否数字字符串 
        private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");              // 是否数字字符串 可带正负号
        private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");            // 是否是浮点数 
        private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //是否是浮点数 可带正负号 
        private static Regex RegNHade = new Regex("^(0|[1-9][0-9]*)$");            //只能输入零和非零开头的数字 
        private static Regex RegEmail = new Regex("^.{1,}@.{1,}\\.[a-zA-Z]{2,3}$");// 邮件地址
        private static Regex RegURL = new Regex("[a-zA-z]+://[^\\s]*"); //验证InternetURL 
        private static Regex RegIp = new Regex("^((23[0-3]|1\\d{2}|[1-9]\\d|[1-9])\\.)((25[0-5]|2[0-4]\\d|1?\\d{1,2})\\.){2}((25[0-5]|2[0-4]\\d|1?\\d{1,2}))$");
        private static Regex RegCHZN = new Regex("[\\u4e00-\\u9fa5]");  //中文检测 
        private static Regex RegEn = new Regex("^[A-Za-z]+$");          //英文检测 
        private static Regex RegMinEn = new Regex("^[a-z]+$");          //小写英文检测 
        private static Regex RegMaxEn = new Regex("^[A-Z]+$");          //大写英文检测 
        private static Regex RegMinMaxEnNumber = new Regex("^[A-Za-z0-9]+$");//只能输入由数字和26个英文字母组成的字符串
        private static Regex RegEnNumber = new Regex("^[a-zA-Z]+[a-zA-Z0-9]{5,20}$");//匹配(字母开头,允许6-20字符,允许字母数字.)是否合法
        private static Regex RegEnAndNumber = new Regex("^[a-zA-Z0-9]{8,20}$");//匹配(8-20字符,允许字母数字)是否合法
        private static Regex RegMobile = new Regex("^((1(3[4-9]|5[012789]|8[78]))|(18[09])|(1(3[0-2]|5[56]|8[56]))|(1[35]3))[0-9]{8}$"); //是否是手机号码的格式
        //private static Regex RegTelePhone = new Regex("(^(0\\d{2})-(\\d{8})$)|(^(0\\d{3})-(\\d{7})$)|(^(0\\d{2})-(\\d{8})$)|(^(0\\d{3})-(\\d{8})$)");//是否为正确的固定电话号码,可验证020-88888888或0668-8888888
        private static Regex RegTelePhone = new Regex("(^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)");//是否为正确的固定电话号码,可验证3-4位区号,7-8位直播号码,1－4位分机号
        private static Regex RegDate = new Regex("^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))$");    // 是否是日期格式
        private static Regex RegYearMonth = new Regex("^(?<year>\\d{2,4})-(?<month>\\d{1,2})$");//年份-月份 yyyy-MM

        #region "固定电话验证"
        /// <summary>
        /// 是否为正确的固定电话号码
        /// </summary>
        /// <param name="inputData">验证的字符串</param>
        /// <returns></returns>
        public static bool IsTelePhone(string inputData)
        {
            Mch = RegTelePhone.Match(inputData);
            return Mch.Success;
        }
        #endregion

        #region "手机号码验证"
        /// <summary>
        /// 是否为正确的手机号码
        /// </summary>
        /// <param name="inputData">验证的字符串</param>
        /// <returns></returns>
        public static bool IsMobile(string inputData)
        {
            Mch = RegMobile.Match(inputData);
            return Mch.Success;
        }
        #endregion

        #region "数字字符串检查"

        /// <summary> 
        /// 是否数字字符串 
        /// </summary> 
        /// <param name="inputData">输入字符串</param> 
        /// <returns></returns> 
        public static bool IsNumber(string inputData)
        {
            Mch = RegNumber.Match(inputData);
            return Mch.Success;
        }
        /// <summary> 
        /// 是否数字字符串,可带正负号.
        /// </summary> 
        /// <param name="inputData">输入字符串</param> 
        /// <returns></returns> 
        public static bool IsNumberSign(string inputData)
        {
            Mch = RegNumberSign.Match(inputData);
            return Mch.Success;
        }
        /// <summary> 
        /// 是否是浮点数 
        /// </summary> 
        /// <param name="inputData">输入字符串</param> 
        /// <returns></returns> 
        public static bool IsDecimal(string inputData)
        {
            Mch = RegDecimal.Match(inputData);
            return Mch.Success;
        }
        /// <summary> 
        /// 是否是浮点数,可带正负号.
        /// </summary> 
        /// <param name="inputData">输入字符串</param> 
        /// <returns></returns> 
        public static bool IsDecimalSign(string inputData)
        {
            Mch = RegDecimalSign.Match(inputData);
            return Mch.Success;
        }

        /// <summary> 
        ///只能输入零和非零开头的数字.
        /// </summary> 
        /// <param name="inputData"></param> 
        /// <returns></returns> 
        public static bool IsHasNHade(string inputData)
        {
            Mch = RegNHade.Match(inputData);
            return Mch.Success;
        }

        /// <summary> 
        ///是否是日期格式
        /// </summary> 
        /// <param name="inputData"></param> 
        /// <returns></returns> 
        public static bool IsDate(string inputData)
        {
            Mch = RegDate.Match(inputData);
            return Mch.Success;
        }
        #endregion

        #region "中文,英文数字组合检测"

        /// <summary> 
        /// 检测是否有中文字符. 
        /// </summary> 
        /// <param name="inputData"></param> 
        /// <returns></returns> 
        public static bool IsHasCHZN(string inputData)
        {
            Mch = RegCHZN.Match(inputData);
            return Mch.Success;
        }

        /// <summary> 
        /// 检测是否有英文字符. 
        /// </summary> 
        /// <param name="inputData"></param> 
        /// <returns></returns> 
        public static bool IsHasEn(string inputData)
        {
            Mch = RegEn.Match(inputData);
            return Mch.Success;
        }

        /// <summary> 
        /// 检测是否有小写英文. 
        /// </summary> 
        /// <param name="inputData"></param> 
        /// <returns></returns> 
        public static bool IsHasMinEn(string inputData)
        {
            Mch = RegMinEn.Match(inputData);
            return Mch.Success;
        }

        /// <summary> 
        /// 检测是否有大写英文 
        /// </summary> 
        /// <param name="inputData"></param> 
        /// <returns></returns> 
        public static bool IsHasMaxEn(string inputData)
        {
            Mch = RegMaxEn.Match(inputData);
            return Mch.Success;
        }

        /// <summary> 
        ///只能输入由数字和26个英文字母组成的字符串 
        /// </summary> 
        /// <param name="inputData"></param> 
        /// <returns></returns> 
        public static bool IsHasMinMaxEnNumber(string inputData)
        {
            Mch = RegMinMaxEnNumber.Match(inputData);
            return Mch.Success;
        }

        /// <summary> 
        ///匹配(字母开头,允许6-20字符,允许字母数字.)是否合法
        /// </summary> 
        /// <param name="inputData"></param> 
        /// <returns></returns> 
        public static bool IsEnNumber(string inputData)
        {
            Mch = RegEnNumber.Match(inputData);
            return Mch.Success;
        }

        /// <summary> 
        ///匹配(8-20字符,允许字母数字)是否合法
        /// </summary> 
        /// <param name="inputData"></param> 
        /// <returns></returns> 
        public static bool IsEnAndNumber(string inputData)
        {
            Mch = RegEnAndNumber.Match(inputData);
            return Mch.Success;
        }

        #endregion

        #region "邮件地址,URL相关等"
        /// <summary> 
        /// 邮件地址 
        /// </summary> 
        /// <param name="inputData">输入字符串</param> 
        /// <returns></returns> 
        public static bool IsEmail(string inputData)
        {
            Mch = RegEmail.Match(inputData);
            return Mch.Success;
        }

        /// <summary> 
        /// URL地址 
        /// </summary> 
        /// <param name="inputData">输入字符串</param> 
        /// <returns></returns> 
        public static bool IsURL(string inputData)
        {
            Mch = RegURL.Match(inputData);
            return Mch.Success;
        }
        /// <summary> 
        /// IP地址 
        /// </summary> 
        /// <param name="inputData">输入字符串</param> 
        /// <returns></returns> 
        public static bool IsIP(string inputData)
        {
            Mch = RegIp.Match(inputData);
            return Mch.Success;
        }

        #endregion

        #region "其他"

        /// <summary> 
        /// 检查字符串最大长度，返回指定长度的字符串 
        /// </summary> 
        /// <param name="sqlInput">输入字符串</param> 
        /// <param name="maxLength">最大长度</param> 
        /// <returns></returns> 
        public static string SqlText(string sqlInput, int maxLength, Boolean Bol)
        {
            if (sqlInput != null & sqlInput != string.Empty)
            {
                sqlInput = sqlInput.Trim();
                if (sqlInput.Length > maxLength)
                {
                    sqlInput = sqlInput.Substring(0, maxLength);
                    if (Bol)
                    {
                        sqlInput += "...";
                    }
                }
            }
            return sqlInput;
        }
        #endregion

        #region  判断控件中的长度是否大于预设值

        /// <summary>
        /// 截断内容长度大于预设值
        /// </summary>
        /// <param name="StrControlValue">验证的内容</param>
        /// <param name="IntLength">预设的长度</param>
        /// <returns></returns>
        public static string CutControlLength(string StrControlValue, int IntLength)
        {
            if (StrControlValue == "")
            {
                return "";
            }
            else
            {
                if (StrControlValue.Length <= IntLength)
                {
                    return StrControlValue;
                }
                else
                {
                    StrControlValue = StrControlValue.Substring(0, IntLength);
                    return StrControlValue;
                }
            }
        }
        #endregion

        #region 验证输入内容是否为合法的日期,日期格式:yyyy-MM
        /// <summary>
        /// 年份-月份 yyyy-MM
        /// </summary>
        /// <param name="inputData">验证的字符串</param>
        /// <returns></returns>
        public static bool IsYearMonth(string inputData)
        {
            Mch = RegYearMonth.Match(inputData);
            return Mch.Success;
        }
        #endregion
    }
}
